﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.BLL;
using WMS.Entity;

namespace WMS.App
{
    public partial class frmIntoOutGoodsLog : Form
    {
        private GoodsLog goodsLog = null;
        private GoodsLogEntity entity = null;

        public frmIntoOutGoodsLog()
        {
            InitializeComponent();

            InitData();
        }

        void InitData()
        {
            goodsLog = new GoodsLog();
            entity = new GoodsLogEntity();
        }

        public void SetGoodsLog(string GoodsName, string GoodsCode, int Type)
        {
            this.txtPrice.Text = "";
            this.txtQuantity.Text = "";
            entity.GoodsCode = GoodsCode;
            entity.Type = Type;
            this.txtName.Text = GoodsName;
            if (Type == 0)
            {
                this.Text = "入库";
            }
            else
            {
                this.Text = "出库";
            }

            IList<GoodsLogEntity> list = goodsLog.GetGoodsLogRecords(int.Parse(GoodsCode));
            var inQuantity = 0;
            var outQuantity = 0;
            foreach (GoodsLogEntity item in list)
            {
                if (item.Type == 0)
                {
                    inQuantity += item.Quantity;
                }
                else if (item.Type == 1)
                {
                    outQuantity += item.Quantity;
                }
            }

            this.txtRemain.Text = (inQuantity - outQuantity).ToString();
        }

        bool ValidateValue()
        {
            bool flag = true;
            decimal price = 0;
            int quantity = 0;
            int remain = int.Parse(this.txtRemain.Text);
            if (decimal.TryParse(this.txtPrice.Text, out price) == false)
            {
                flag = false;
                MessageBoxCommon.Warnings("价格只能填写数字！例如23.3");
            }
            else if (int.TryParse(this.txtQuantity.Text, out quantity) == false)
            {
                MessageBoxCommon.Warnings("数量只能填写数字！");
                flag = false;
            }
            if (entity.Type == 1 && flag==true)
            {
                if (remain <= 0)
                {
                    MessageBoxCommon.Warnings("库存量不足！");
                    flag = false;
                }
                else if ((remain - quantity) < 0)
                {
                    MessageBoxCommon.Warnings("出库的数量大于剩余数量！");
                    this.txtQuantity.Focus();
                    flag = false;
                }
            }
            entity.Price = price;
            entity.Quantity = quantity;
            return flag;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateValue())
            {
                goodsLog.Add(entity);
                this.btnSave.Enabled = false;
                MessageBoxCommon.Success();
            }
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar)) e.Handled = true;

            if (e.KeyChar == '\b' || e.KeyChar == '.')
            {
                if (((TextBox)sender).Text.IndexOf(".") >= 0 && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
            }
        }

        private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar)) e.Handled = true;

            if (e.KeyChar == '\b') e.Handled = false;
        }
    }
}
